<?php
//当前页
$page=isset($_GET['page']) ? $_GET['page'] : 1;
//每页条数
$size=isset($_GET['limit']) ? $_GET['limit'] : 6;
//接收ID值
$id=isset($_GET['id']) ? $_GET['id'] : "";
//接收标题值
$title=isset($_GET['title']) ? $_GET['title'] : "";
//接收状态值
$status=isset($_GET['status']) ? $_GET['status'] : "";

$where="1";
if($id!=""){
    $where.=" and id=$id";
}
if($title!=""){
    $where.=" and title like '%$title%'";
}
if($status!=""){
    $where.=" and status like '%$status%'";
}

//初始化
$redis = new Redis();
//连接
$redis->connect("127.0.0.1",6379);
if($redis->hExists("page","page$page") && $where=="1"){
   echo $redis->hGet("page","page$page");
}else{

//连接数据库
$dbh = new PDO('mysql:host=127.0.0.1;dbname=1705a', "root", "root");
//设置字符集
$dbh->exec("set names utf8");
//sql语句
$sql="select * from article";
//执行
$stmt=$dbh->query($sql);
//总条数
$rowcount=$stmt->rowCount();
//偏移量
$limit=($page-1)*$size;
//sql2语句
$sql2="select * from article where $where limit $limit,$size";
//执行
$stmt2=$dbh->query($sql2);
//结果集
$data=$stmt2->fetchAll(2);

if(!$data){
    $arr['code']=1;
    $arr['msg']="no";
    $arr['data']="";
    echo json_encode($arr);
}
$arr['code']=0;
$arr['msg']="yes";
$arr['data']=$data;
//传总条数
$arr['count']=$rowcount;
$arr1=json_encode($arr);
echo $arr1;

if($where=="1"){
    $redis->hSet("page","page$page",$arr1);
}

}


?>